package com.roy.DubboSpringClient.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.roy.DubboRPCDemo.bean.RPCResBean;
import com.roy.DubboRPCDemo.bean.UserBean;
import com.roy.DubboRPCDemo.protocol.MyService;

@Controller()
public class UserController {

	@Reference(timeout = 1000, retries = 0, cluster = "failfast",check=true)
	private MyService service;

	@ResponseBody
	@RequestMapping("/")
	public String doService() {
		Long starttime = (new Date()).getTime();
		String result = service.sayHello("world");
		System.out.println(result);
		System.out.println("基础字段类型服务调用");
		for (int i = 0; i < 10; i++) {
			starttime = (new Date()).getTime();
			System.out.println(
					service.baseTypeService(100, "baseTypeUser", "123123123"));
			System.out
					.println("服务调用耗时：" + ((new Date()).getTime() - starttime));
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Map类型服务调用");
		for (int i = 0; i < 10; i++) {
			starttime = (new Date()).getTime();
			Map<String, String> userInfo = new HashMap<>();
			userInfo.put("userId", "" + i);
			userInfo.put("userName", "UserName : " + i);
			userInfo.put("userPassword", "UserPassword : " + i);
			System.out.println(service.mapTypeService(userInfo));
			System.out.println("服务调用耗时：" + ((new Date()).getTime() - starttime));
		}
		System.out.println("对象类型服务调用");
		for (int i = 0; i < 10; i++) {
			starttime = (new Date()).getTime();
			UserBean user = new UserBean();
			user.setUserId(i);
			user.setUserName("UserName:" + i);
			user.setUserPassword("UserPassword:" + i);
			System.out.println(service.objTypeService(user));
			System.out.println("服务调用耗时：" + ((new Date()).getTime() - starttime));
		}
		System.out.println("对象数组类型服务调用");
		starttime = (new Date()).getTime();
		List<UserBean> users = new ArrayList<UserBean>();
		for (int i = 0; i < 10; i++) {
			UserBean user = new UserBean();
			user.setUserId(i);
			user.setUserName("UserName:" + i);
			user.setUserPassword("UserPassword:" + i);
			users.add(user);
		}
		List<UserBean> userBeanArray = service.objArrayService(users);
		System.out.println(userBeanArray);
		System.out.println("服务调用耗时：" + ((new Date()).getTime() - starttime));

		System.out.println("框架类型服务调用");

		System.out.println("调用initUser服务");
		starttime = (new Date()).getTime();
		String[] params = new String[]{"101", "usreName"};
		RPCResBean initUserRes = service.frameworkService("initUser", params);
		System.out.println("调用服务initUser完成,res= " + initUserRes);
		System.out.println("服务调用耗时：" + ((new Date()).getTime() - starttime));

		System.out.println("调用destroyUser服务");
		starttime = (new Date()).getTime();
		params = new String[]{"102"};
		RPCResBean desUserRes = service.frameworkService("destroyUser", params);
		System.out.println("调用服务destroyUser完成,res= " + desUserRes);
		System.out.println("服务调用耗时："+((new Date()).getTime() - starttime));
		
		return "Test Completed,Please check the console log";
	}
}
